package com.guigu.erp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.StorageGatherDto;
import com.guigu.erp.mapper.StorageGatherMapper;
import com.guigu.erp.pojo.StorageGather;
import com.guigu.erp.pojo.StorageGatherDetails;
import com.guigu.erp.service.StorageGatherDetailsService;
import com.guigu.erp.service.StorageGatherService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.StorageGatherVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class StorageGatherServiceImpl extends ServiceImpl<StorageGatherMapper, StorageGather>
    implements StorageGatherService {

    @Autowired
    private StorageGatherMapper storageGatherMapper;

    @Autowired
    private StorageGatherDetailsService storageGatherDetailsService;

    @Override
    public String getMaxDesignId() {
        StorageGather storageGather = storageGatherMapper.queryMaxModule();
        if (storageGather == null){
            return null;
        }
        String designId = storageGather.getGatherId();
        return designId;
    }

    @Override
    @Transactional
    public boolean addInboundWithDetails(StorageGatherDto dto) {
        System.out.println(dto);
        this.save(dto);
        boolean flag=false;
        //拿到明细
        List<StorageGatherDetails> details = dto.getDetails();

        for (int i=0;i<details.size();i++){
            //给明细添加数据
            StorageGatherDetails gatherDetails = details.get(i);
            //给id置为null
            gatherDetails.setId(null);
            //给父类id设值
            gatherDetails.setParentId(dto.getId());
            //设置入库标志 K002-1: 已登记
            gatherDetails.setGatherTag("K002-1");
            //设置确认入库件数
            gatherDetails.setGatheredAmount(new BigDecimal(0));
            //调用添加的方法
            flag = storageGatherDetailsService.save(gatherDetails);
        }
        return flag;
    }

    @Override
    public PageUtils<StorageGather> getAllStorage(StorageGatherVo vo, Integer pageNo, Integer pageSize) {
        //封装查询条件
        QueryWrapper<StorageGather> wrapper = new QueryWrapper<>();
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_id())){
                wrapper.like("GATHER_ID","%"+vo.getSearch_id()+"%");
            }
            if(!StringUtils.isEmpty(vo.getSearch_reson())){
                wrapper.eq("REASON",vo.getSearch_reson());
            }
            if(!StringUtils.isEmpty(vo.getSearch_statue())){
                wrapper.eq("CHECK_TAG",vo.getSearch_statue());
            }
            if (!com.alibaba.druid.util.StringUtils.isEmpty(vo.getSearch_regtime1())) {
                wrapper.ge("REGISTER_TIME", vo.getSearch_regtime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_regtime2())) {
                wrapper.le("REGISTER_TIME", vo.getSearch_regtime2());
            }
        }
        PageHelper.startPage(pageNo,pageSize);
        List<StorageGather> list = this.list(wrapper);
        PageInfo<StorageGather> pageInfo = new PageInfo<>(list);
        PageUtils<StorageGather> pageUtils = new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }

    @Override
    public PageUtils<StorageGather> getStorageAuditPass(StorageGatherVo vo, Integer pageNo, Integer pageSize) {
        //封装查询条件
        QueryWrapper<StorageGather> wrapper = new QueryWrapper<>();
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_reson())){
                wrapper.eq("REASON",vo.getSearch_reson());
            }
            if (!com.alibaba.druid.util.StringUtils.isEmpty(vo.getSearch_regtime1())) {
                wrapper.ge("REGISTER_TIME", vo.getSearch_regtime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_regtime2())) {
                wrapper.le("REGISTER_TIME", vo.getSearch_regtime2());
            }
        }
        wrapper.eq("CHECK_TAG","S001-1");
        wrapper.eq("STORE_TAG","K002-1");

        PageHelper.startPage(pageNo,pageSize);
        List<StorageGather> list = this.list(wrapper);
        PageInfo<StorageGather> pageInfo = new PageInfo<>(list);
        PageUtils<StorageGather> pageUtils = new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }
}
